package com.eleadmin.goods.controller;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eleadmin.common.core.web.PageParam;
import com.eleadmin.common.core.web.PageResult;
import com.eleadmin.common.system.entity.Role;
import com.eleadmin.common.system.entity.User;
import com.eleadmin.common.system.param.RoleParam;
import com.eleadmin.common.system.param.UserParam;
import com.eleadmin.goods.param.GoodsImportParam;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.eleadmin.common.core.web.ApiResult;
import com.eleadmin.common.core.web.BaseController;
import com.eleadmin.goods.entity.Goods;
import com.eleadmin.goods.param.GoodsParam;
import com.eleadmin.goods.service.GoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;


@Api(tags = "商品管理")
@RestController
@RequestMapping("/api/product/goods")
public class GoodsController extends BaseController {

    @Autowired
    private GoodsService goodsService;

    @ApiOperation("分页查询商品")
    @GetMapping("/page")
    public ApiResult<PageResult<Goods>> page(GoodsParam param) {
        PageParam<Goods, GoodsParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(goodsService.page(page, page.getWrapper()));
    }

    @ApiOperation("添加商品")
    @PostMapping()
    public ApiResult<?> save(@RequestBody Goods Goods) {
        if (goodsService.save(Goods)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @ApiOperation("修改角色")
    @PutMapping()
    public ApiResult<?> update(@RequestBody Goods goods) {
        if (goodsService.updateById(goods)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @ApiOperation("删除商品")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (goodsService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @ApiOperation("批量删除角色")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (goodsService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    /**
     * 导入商品信息
     * @param file excel文件
     */
    @ApiOperation("导入商品信息")
    @Transactional(rollbackFor = {Exception.class})
    @PostMapping("/import")
    public ApiResult<List<String>> importGoods(MultipartFile file) {
        ImportParams importParams = new ImportParams();
        try {
            // 将excel文件转换为商品导入参数对象列表
            List<GoodsImportParam> list = ExcelImportUtil.importExcel(file.getInputStream(),
                    GoodsImportParam.class, importParams);

            // 将商品导入参数对象列表保存到数据库中
            // 注意：这里需要根据实际情况调用Service方法保存商品信息
            // 假设这里调用一个名为goodsService的Service方法来保存商品信息
            boolean result = goodsService.saveBatch(convertToGoodsList(list));

            if (result) {
                return success("导入成功",null);
            } else {
                return fail("导入失败",null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return fail("导入失败",null);
        }
    }

    /**
     * 将商品导入参数对象列表转换为商品对象列表
     * @param importParams 商品导入参数对象列表
     * @return 商品对象列表
     */
    private List<Goods> convertToGoodsList(List<GoodsImportParam> importParams) {
        List<Goods> goodsList = new ArrayList<>();
        for (GoodsImportParam param : importParams) {
            Goods goods = new Goods();
            // 设置商品信息
            goods.setTitle(param.getTitle());
            goods.setGoodType(param.getGoodType());
            goods.setPrice(param.getPrice());
            // 使用构造函数将 java.util.Date 转换为 java.sql.Timestamp
            goods.setCreateTime(new Timestamp(param.getCreateTime().getTime()));
            goods.setCreatorId(param.getCreatorId());
            // 使用构造函数将 java.util.Date 转换为 java.sql.Timestamp
            goods.setChangeTime(new Timestamp(param.getChangeTime().getTime()));
            goods.setChangerId(param.getChangerId());
            goods.setTenantId(param.getTenantId());
            // 添加到商品对象列表中
            goodsList.add(goods);
        }
        return goodsList;
    }



}
